#!/bin/bash
#计数请求失败主机
# 设置各主机访问次数变量
#path_fs="$1"
#key="$2"
read -p "请输入日志文件路径：" path_fs
read -p "请输入匹配关键字：" key
# 判断路径是否为空 
if [ ! ${path_fs} ];then
  echo "================================>"
  echo "日志文件路径不能为空！！！"
  exit
fi

rule=$(echo "awk --re-interval '{match(\$0,/([0-9]{1,3}\.){3}[0-9]{1,3}/,a); print a[0]}'") # 设置匹配规则
c_s=`cat ${path_fs} | awk "/(${key})/" | eval ${rule} | sort | uniq -c | sort -nr | awk -F" " '{print "主机：",$2,"访问次数："$1}' OFS="---->"` # 逐行显示大小排序
#sleep 2
# 判断是否匹配到内容
if [ ! "${c_s}" ];then
  echo "================================>"
  echo "没有匹配到内容，请确认匹配关键字是否正确！！！"
  exit
fi

z_dcs=`cat ${path_fs} | awk "/(${key})/" | eval ${rule} | sort | uniq -c | sort -nr | head -1 | awk -F " " '{print $1}'`

z_dcszj=`cat ${path_fs} | awk "/(${key})/" | eval ${rule} | sort | uniq -c | sort -nr | head -1 | awk -F " " '{print $2}'`

counts=`cat ${path_fs} | awk "/(${key})/" | eval ${rule} | sort | uniq -c | sort -nr | wc -l`

z_fwl=`cat ${path_fs} | awk "/(${key})/" | eval ${rule} | wc -l`
# 遍历回显
for C_S in ${c_s}
do
  echo ${C_S}
done
echo "=================================================================>"
echo "主机访问总台数：${counts}---->总访问量：${z_fwl}
最大访问主机：${z_dcszj}---->共访问：${z_dcs}"

